package top.itaso.lib.common;

import android.text.TextUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description: 手机号、身份证号、银行卡号 的 * 号保密工具
 * @Author itsdf07
 * @Date 2022/09/11
 */
public class ReplaceWithStarUtils {

    /**
     * 根据数字字符的不同长度，来进行替换 ，达到保密效果
     *
     * @param replaceNumber
     * @return
     */
    public static String replaceNumber2Star(String replaceNumber) {
        if (TextUtils.isEmpty(replaceNumber)) {
            return "";
        }
        String afterReplaced = "";
        int contentLength = replaceNumber.length();

        if (contentLength == 1) {
            return replaceNumber;
        } else if (contentLength == 2) {
            afterReplaced = replaceAction(replaceNumber, "(?<=\\d{0})\\d(?=\\d{1})");
        } else if (contentLength >= 3 && contentLength <= 6) {
            afterReplaced = replaceAction(replaceNumber, "(?<=\\d{1})\\d(?=\\d{1})");//保留前1后1数字，其他以*代替
        } else if (contentLength >= 7 && contentLength <= 10) {
            afterReplaced = replaceAction(replaceNumber, "(?<=\\d{3})\\d(?=\\d{3})");//保留前3后3数字，其他以*代替
        } else if (contentLength >= 11 && contentLength <= 17) {
            afterReplaced = replaceAction(replaceNumber, "(?<=\\d{3})\\d(?=\\d{4})");//保留前3后4数字，其他以*代替
        } else {
            afterReplaced = replaceAction(replaceNumber, "(?<=\\d{4})\\d(?=\\d{4})");//保留前4后4数字，其他以*代替
        }

        return afterReplaced;
    }

    /**
     * 根据手机号的不同长度，来进行替换 ，达到保密效果
     *
     * @param replaceContent
     * @return 替换后的内容
     */
    public static String replacePhone2Star(String replaceContent) {
        if (TextUtils.isEmpty(replaceContent)) {
            return "";
        }
        Pattern pattern = Pattern.compile("(1[3-9][0-9]{9})|((\\+\\d{2})-(1[3-9][0-9]{9}))|((\\+\\d{2})(1[3-9][0-9]{9}))");
        Matcher matcher = pattern.matcher(replaceContent);
        if (!matcher.find()) {
            return "Illegal phone number";//不合法的移动电话号码
        }

        return replaceAction(replaceContent, "(?<=\\d{3})\\d(?=\\d{4})");//保留前3后4数字，其他以*代替;

    }

    /**
     * 身份证号替换，保留前四位和后四位
     *
     * @param idCard 身份证号
     * @return
     */
    public static String replaceIDCard2Star(String idCard) {
        if (TextUtils.isEmpty(idCard) || idCard.length() != 18) {
            return "Illegal ID number";//不合法的身份证号码
        }
        return replaceAction(idCard, "(?<=\\d{4})\\d(?=\\d{4})");//保留前4后4数字，其他以*代替
    }

    /**
     * 银行卡替换，保留前4位后4位
     *
     * @param bankCard 银行卡号
     * @return
     */
    public static String replaceBankCard2Star(String bankCard) {

        if (bankCard.isEmpty() || bankCard == null) {
            return null;
        } else {
            return replaceAction(bankCard, "(?<=\\d{4})\\d(?=\\d{4})");
        }
    }

    /**
     * 实际替换动作
     *
     * @param replaceContent 替换内容
     * @param regex          正则表达式
     * @return
     */
    private static String replaceAction(String replaceContent, String regex) {
        return replaceContent.replaceAll(regex, "*");
    }
}
